#include "userMain.h"
#include "userSHT20driver.h"

#define     SHT20_WRITE_ADDR    0x80
#define     SHT20_READ_ADDR     0x81
#define     SUCCESS             0x01
#define     FAIL                0x00

uint32_t userHumidity,userTemperature;
uint8_t userHumidityNow,userTemperatureNow;
uint8_t userHumidityPre,userTemperaturePre;
uint8_t userSHT20measureFunc(uint8_t userCommand)
{
    uint8_t userTmp1,userTmp2;
    uint16_t userST;
    DE_Bx_IIC_INITIAL;
    DE_Bx_IIC_START;
    Bx_Write8Bit(SHT20_WRITE_ADDR);
    if(DE_Bx_TESTACK)
    {
        DE_Bx_IIC_STOP;
        DE_Bx_IIC_END;
        return FAIL;
    }
    Bx_Write8Bit(userCommand);
    if(DE_Bx_TESTACK)
    {
        DE_Bx_IIC_STOP;
        DE_Bx_IIC_END;
        return FAIL;
    }
    userDelay2ms(50);
    DE_Bx_IIC_START;
    Bx_Write8Bit(SHT20_READ_ADDR);
    if(DE_Bx_TESTACK)
    {
        DE_Bx_IIC_STOP;
        DE_Bx_IIC_END;
        return FAIL;
    }
    //if(userCommand == 0xF3)
    //{
    //  userDelay2ms(43);
    //}
    //if(userCommand == 0xF5)
    //{
    //  userDelay2ms(15);
    //}
    userTmp1 = DE_Bx_READ8BIT;
    DE_Bx_ACK;
    userTmp2 = DE_Bx_READ8BIT;
    DE_Bx_NOACK;
    DE_Bx_IIC_STOP;
    DE_Bx_IIC_END;

    userST = (userTmp1 << 8) | userTmp2;
    userST &= ~0x0003;
    if(userCommand == 0xF3)  // no hold master, temperature measure
    {  
        userTemperature = userST * 17572;
        userTemperature = userTemperature / 65536 - 4685;
    }
    if(userCommand == 0xF5)  // no hold master, humidity measure
    {
        userHumidity = userST * 12500;
        userHumidity = userHumidity / 65536 - 600;
    }
    return SUCCESS;
}

#define     SOFT_RESET_CMD      0xFE
uint8_t userSHT20softResetFunc(void)
{
    userDelay2ms(8);
    DE_Bx_IIC_INITIAL;
    DE_Bx_IIC_START;
    Bx_Write8Bit(SHT20_WRITE_ADDR);
    if(DE_Bx_TESTACK)
    {
        DE_Bx_IIC_STOP;
        DE_Bx_IIC_END;
        return FAIL;
    }
    Bx_Write8Bit(SOFT_RESET_CMD);
    if(DE_Bx_TESTACK)
    {
        DE_Bx_IIC_STOP;
        DE_Bx_IIC_END;
        return FAIL;
    }
    userDelay2ms(8);
    DE_Bx_IIC_STOP;
    DE_Bx_IIC_END;
    return SUCCESS;
}

uint8_t userSHT20resolutionSetFunc(uint8_t initialValue)
{
    DE_Bx_IIC_INITIAL;
    DE_Bx_IIC_START;
    Bx_Write8Bit(SHT20_WRITE_ADDR);
    if(DE_Bx_TESTACK)
    {
        DE_Bx_IIC_STOP;
        DE_Bx_IIC_END;
        return FAIL;
    }
    Bx_Write8Bit(0xE6);
    if(DE_Bx_TESTACK)
    {
        DE_Bx_IIC_STOP;
        DE_Bx_IIC_END;
        return FAIL;
    }
    Bx_Write8Bit(initialValue);
    if(DE_Bx_TESTACK)
    {
        DE_Bx_IIC_STOP;
        DE_Bx_IIC_END;
        return FAIL;
    }
    DE_Bx_IIC_STOP;
    DE_Bx_IIC_END;
    return SUCCESS;
}

void userSHT20initFunc(void)
{
    userSHT20softResetFunc();
    userSHT20resolutionSetFunc(0x02);
}

void userGetDataFromSHT20Func(void)
{
    userSHT20initFunc();
    userSHT20measureFunc(0xF3);
    userSHT20measureFunc(0xF5);
}
uint8_t userHTreportCnt = 20;
void userReportTickSHT20(void)
{
    if(userHTreportCnt)
        userHTreportCnt--;
}
extern uint8_t uBmaDetectFlag;
void userReportDataFromSHT20(void)
{
    if(userHTreportCnt) return;
    userHTreportCnt = 100;
    uBmaDetectFlag = 1;
    userGetDataFromSHT20Func();
    uBmaDetectFlag = 0;
    userHumidityNow = userHumidity/100;
    userTemperatureNow = userTemperature / 100;
    if( (userTemperatureNow!=userTemperaturePre) || \
        (userHumidityNow > (userHumidityPre+4)) ||\
        (userHumidityNow < (userHumidityPre-4)) )
    {
        userHumidityPre = userHumidityNow;
        userTemperaturePre = userTemperatureNow;
        userSendHTinfoFunc();
    }
}